package com.threeidiots.findit;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.threeidiots.findit.db.Record;

/**
 * This activity is used when the user wants to get the thing they
 * saved(i.e.click the item in main activity).
 * 
 * @author Elvis
 * 
 */
public class GetItemActivity extends Activity implements OnClickListener,
		OnTouchListener {
	private static final String TAG = "GetItemActivity";
	private TextView mThing = null;
	private ImageView mPlayVoiceRecord = null;
	private ImageView mNavigate = null;

	private TextView mDescription = null;
	private ImageView mPicture = null;
	private TextView mFindIt = null;
	private TextView mBack = null;
	private Record mRecord = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get);
		Intent intent = getIntent();
		mRecord = FindIt.getRecord(intent.getIntExtra("item", 0));

		mThing = (TextView) findViewById(R.id.get_thing);
		mThing.setText(mRecord.getThing());
		mPlayVoiceRecord = (ImageView) findViewById(R.id.play_voice_record);
		mPlayVoiceRecord.setOnClickListener(this);
		mPlayVoiceRecord.setOnTouchListener(this);
		mNavigate = (ImageView) findViewById(R.id.navigate);
		mNavigate.setOnTouchListener(this);
		mNavigate.setOnClickListener(this);

		// show the description;if the tag equals "",don't show it
		mDescription = (TextView) findViewById(R.id.get_description);
		if (mRecord.getTag().equals("")) {
			mDescription.setVisibility(View.INVISIBLE);
		}
		mDescription.setText(mRecord.getTag());
		// show the picture,it has the default picture
		mPicture = (ImageView) findViewById(R.id.get_image);
		String imagePath = mRecord.getPicture();
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 2;
		Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
		mPicture.setImageBitmap(bitmap);

		mFindIt = (TextView) findViewById(R.id.find_it);
		mFindIt.setOnTouchListener(this);
		mFindIt.setOnClickListener(this);
		mBack = (TextView) findViewById(R.id.back);
		mBack.setOnTouchListener(this);
		mBack.setOnClickListener(this);
	}

	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			v.setBackgroundColor(getResources().getColor(
					R.color.top_imageView_pressed));
			break;
		case MotionEvent.ACTION_UP:
			v.setBackgroundColor(getResources()
					.getColor(R.color.top_background));
			break;
		default:
			break;
		}
		return false;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.play_voice_record:
			String recordFileName = mRecord.getTape();
			if (recordFileName == null) {
				Toast.makeText(this, getString(R.string.hint_no_tape),
						Toast.LENGTH_SHORT).show();
				break;
			}
			MediaPlayer player = new MediaPlayer();
			try {
				player.setDataSource(recordFileName);
				player.prepare();
				player.start();
			} catch (IOException e) {
				Log.e(TAG, "prepare() failed");
			}
			break;
		case R.id.navigate:
			if (mRecord.getLatitude() == null || mRecord.getLongitude() == null) {
				Toast.makeText(this, getString(R.string.hint_no_navigate),
						Toast.LENGTH_SHORT).show();
				break;
			}

			FindItLocation loc = new FindItLocation(this);
			if (!loc.isGPSon()) {
				AlertDialog.Builder builder = new Builder(this);
				builder.setTitle(this.getResources().getString(
						R.string.radar_attention));
				builder.setMessage(this.getResources().getString(
						R.string.radar_message));
				builder.setPositiveButton(
						this.getResources().getString(R.string.radar_ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								// TODO Auto-generated method stub
								startRadar();
								Intent GPS = new Intent(
										Settings.ACTION_SECURITY_SETTINGS);
								startActivity(GPS);
							}
						});
				builder.setNegativeButton(
						this.getResources().getString(R.string.radar_cancel),
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								startRadar();
							}

						});
				builder.show();
			} else {
				this.startRadar();
			}

			break;
		case R.id.find_it:
			mRecord.setPickTime(new SimpleDateFormat("yyyyMMdd_HHmmss")
					.format(new Date()));
			mRecord.setHistory(true);
			mRecord.update();
			this.finish();
			break;
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

	private void startRadar() {
		Intent i = new Intent("com.threeidiots.findit.radar.SHOW_RADAR");
		i.putExtra("latitude", mRecord.getLatitude());
		i.putExtra("longitude", mRecord.getLongitude());
		startActivity(i);
	}
}
